<template>
  <div class="register-container">
    <div class="register-content">
      <div class="register-right">
        <div class="register-box">
          <h2>新用户注册</h2>
          <el-form
            :model="user"
            class="register-form"
            ref="baseForm"
            :rules="rules"
          >
            <el-form-item prop="username">
              <el-input
                v-model="user.username"
                placeholder="请输入用户名"
                :prefix-icon="User"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="user.password"
                type="password"
                placeholder="请输入密码"
                :prefix-icon="Lock"
              />
            </el-form-item>
            <el-form-item prop="confirmPassword">
              <el-input
                v-model="user.confirmPassword"
                type="password"
                placeholder="请确认密码"
                :prefix-icon="Lock"
              />
            </el-form-item>
            <div class="login-link">
              已有账号？
              <el-button @click="goToLogin">立即登录</el-button>
            </div>
            <el-form-item>
              <el-button class="register-button" @click="submitForm">
                注册
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <video ref="myVideo" autoplay muted loop id="myVideo">
      <source src="../../public/26大数据智慧交通.mp4" />
    </video>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { User, Lock } from '@element-plus/icons-vue';
import { registerUser } from '@/apis/index';

const router = useRouter();

const myVideo = ref(null);

const baseForm = ref(null);

// myVideo.value.playbackRate = 0.5;

const user = ref({
  username: '',
  password: '',
  confirmPassword: '',
  type: 'user',
});

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'));
  } else if (!/^[a-zA-Z0-9_]{6,12}$/.test(value)) {
    callback(new Error('密码格式不正确（6-12位，仅允许字母、数字和下划线）'));
  } else {
    callback();
  }
};

const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'));
  } else if (value !== user.value.password) {
    callback(new Error('两次输入密码不一致!'));
  } else {
    callback();
  }
};

const validateUsername = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入用户名'));
  } else if (!/^[a-zA-Z0-9_-]{2,7}$/.test(value)) {
    callback(
      new Error('用户名格式不正确（2-7位，仅允许字母、数字、下划线和中划线）')
    );
  } else {
    callback();
  }
};

const rules = {
  username: [{ validator: validateUsername, trigger: 'blur' }],
  password: [{ validator: validatePass, trigger: 'blur' }],
  confirmPassword: [{ validator: validatePass2, trigger: 'blur' }],
};

const goToLogin = () => {
  router.push('/login');
};

const submitForm = async () => {
  if (!baseForm.value) return;
  await baseForm.value.validate(async (valid) => {
    if (valid) {
      registerUser(user.value).then((data) => {
        if (data.data.status == 1) {
          ElMessage.success('注册成功,三秒后自动跳转至登录页面');
          setTimeout(() => {
            router.push('/login');
          }, 3000);
        } else {
          ElMessage.error('用户名已存在');
        }
      });
    }
  });
};

onMounted(() => {
  if (myVideo.value) {
    myVideo.value.playbackRate = 0.5;
  }
});
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  background-color: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.register-content {
  width: 380px;
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  display: flex;
  overflow: hidden;
}

.register-left {
  flex: 1;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  padding: 60px;
  display: flex;
  align-items: center;
  color: #fff;
}

.register-info {
  max-width: 400px;
}

.register-info h1 {
  font-size: 36px;
  margin-bottom: 20px;
  font-weight: 600;
}

.register-info p {
  font-size: 18px;
  margin-bottom: 40px;
  opacity: 0.9;
}

.feature-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 16px;
}

.feature-item .el-icon {
  font-size: 24px;
}

.register-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.register-box {
  width: 100%;
  max-width: 400px;
}

.register-box h2 {
  text-align: center;
  margin-bottom: 40px;
  color: #303133;
  font-size: 28px;
  font-weight: 600;
}

.register-form {
  margin-top: 20px;
}

.register-form :deep(.el-input__wrapper) {
  box-shadow: 0 0 0 1px #dcdfe6 inset;
  height: 44px;
}

.register-form :deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #dcdfe6 inset;
}

.register-form :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #dcdfe6 inset;
}

.register-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  margin-top: 10px;
  color: white;
  background-color: rgb(14, 43, 100);
  border: none;
}

.register-button:hover {
  color: white;
}

.login-link {
  text-align: center;
  margin-top: 20px;
  color: #606266;
}

.login-link .el-button {
  padding: 0 5px;
  border: none;
  margin-left: 5px;
  color: #1890ff;
}

.login-link .el-button:hover {
  color: #5cdbd3;
  background-color: white;
}

#myVideo {
  position: fixed;
  right: 0;
  bottom: 0;
  min-width: 100%;
  min-height: 100%;
  z-index: -2;
}

@media screen and (max-width: 768px) {
  .register-content {
    width: 100%;
    height: auto;
    flex-direction: column;
  }

  .register-left {
    padding: 40px;
  }

  .register-right {
    padding: 40px 20px;
  }
}
</style>
